package com.jiadeyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiadeyu.dto.ArticlePageDTO;
import com.jiadeyu.mapper.ArticleMapper;
import com.jiadeyu.pojo.Article;
import com.jiadeyu.pojo.Result;
import com.jiadeyu.service.ArticleService;
import com.jiadeyu.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 文章相关
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @Resource private ArticleMapper articleMapper;

    /**
     * 新增文章
     * @param article 文章
     * @return 新增成功消息
     */
    @PostMapping("/add")
    public Result <String> add(@RequestBody @Validated(Article.Add.class) Article article) {
        articleService.add(article);
        return Result.success("新增文章成功");
    }

    /**
     * 获取文章列表
     * @return 文章列表
     */
    @GetMapping("/list")
    public IPage<Article> list(ArticlePageDTO pageDTO, @RequestParam(required = false) Integer categoryId, @RequestParam(required = false) String state) {
        IPage<Article> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        return articleService.list(page, categoryId, state);
    }
}
